﻿frmSearch = 
{
	// Carga Inicial

	OnLoad : function ()
	{
		this.txtQueryText = document.getElementById('txtQueryText');
		this.txtQueryText.focus ();
		
		this.btnQuery = document.getElementById ("btnQuery");
		
		this.divSearchResults = document.getElementById ('divSearchResults');
		
		this.divAdvancedSearch = document.getElementById ('divAdvancedSearch');
		this.chkSeeAdvancedSearch = document.getElementById ('chkSeeAdvancedSearch');
		this.lblSeeAdvancedSearch = document.getElementById ('lblSeeAdvancedSearch');


		this.ddlMonitoredDirectory = document.getElementById ('ddlMonitoredDirectory');
		this.ddlSubCatalog = document.getElementById ('ddlSubCatalog');
		this.ddlLevelOne = document.getElementById ('ddlLevelOne');
		this.ddlLevelTwo = document.getElementById ('ddlLevelTwo');
		this.ddlLevelThree = document.getElementById ('ddlLevelThree');
		this.ddlLevelFour = document.getElementById ('ddlLevelFour');
		this.ddlLevelFive = document.getElementById ('ddlLevelFive');
		
		this.txtLevelSix = document.getElementById ('txtLevelSix');

		this.txtEnterpriseName = document.getElementById ('txtEnterpriseName');
		this.txtEnterpriseIdentificationNumber = document.getElementById ('txtEnterpriseIdentificationNumber');


		this.reqSearch = CreateXMLHttpRequest ("Search.ashx", "SearchFiles", this.OnSearchComplete);
		
		this.reqGetList = CreateXMLHttpRequest ("Search.ashx", "GetList", OnGetListComplete);


		// Inicializar		
		Hide (this.divAdvancedSearch);
		
		ClearList (this.ddlMonitoredDirectory, 'Cargando...');
		this.reqGetList.Call ({ pListName: "MonitoredDirectory" }, this.ddlMonitoredDirectory);		
		
		this.Clear ();



		this.currentQueryText = "";
		this.currentPageIndex = 0;

		this.currentPageSize = 12;
	},


	Clear : function ()
	{
		this.txtQueryText.value = '';
		this.divSearchResults.innerHTML = '';

		this.ClearAdvanced (true);
	},

	ClearAdvanced : function (pIncludeMonDir)
	{
		if (pIncludeMonDir)
		{
			this.ddlMonitoredDirectory.value = '';
		}

		ClearList (this.ddlSubCatalog);
		ClearList (this.ddlLevelOne);
		ClearList (this.ddlLevelTwo);
		ClearList (this.ddlLevelThree);
		ClearList (this.ddlLevelFour);
		ClearList (this.ddlLevelFive);

		this.txtEnterpriseName.value = '';
		this.txtEnterpriseIdentificationNumber.value = '';
		this.txtLevelSix.value = '';
	},


	// Presentacion

	EnableSearch : function (pIsEnabled)
	{
		this.txtQueryText.disabled = !pIsEnabled;
		this.btnQuery.disabled = !pIsEnabled;
		this.chkSeeAdvancedSearch.disabled = !pIsEnabled;
		this.lblSeeAdvancedSearch.disabled = !pIsEnabled;
	},


	// Funciones

	ShowHideAdvancedSearch : function ()
	{
		if (this.chkSeeAdvancedSearch.checked)
		{
			this.lblSeeAdvancedSearch.innerHTML = "Ocultar Búsqueda Avanzada";
		}
		else
		{
			this.lblSeeAdvancedSearch.innerHTML = "Ver Búsqueda Avanzada";
		}
		
		ShowHide (this.divAdvancedSearch);
	},


	OnChange_MonitoredDirectory : function ()
	{
		this.ClearAdvanced ();

		if (this.ddlMonitoredDirectory.value != '')
		{
			this.OnChange_LoadAdvanced ();
		}
	},


	OnChange_LoadAdvanced : function ()
	{
		var filter = this.GetListFilter ();
			
		if (this.ddlSubCatalog.value == '')
		{
			filter.pListName = "SubCatalog";
			this.reqGetList.Call (filter, this.ddlSubCatalog);
		}

		filter.pListName = "FileReferenceLevel";
		
		if (this.ddlLevelOne.value == '')
		{
			filter.pLevel = 1;
			this.reqGetList.Call (filter, this.ddlLevelOne);
		}
			
		if (this.ddlLevelTwo.value == '')
		{
			filter.pLevel = 2;
			this.reqGetList.Call (filter, this.ddlLevelTwo);
		}

		if (this.ddlLevelThree.value == '')
		{
			filter.pLevel = 3;
			this.reqGetList.Call (filter, this.ddlLevelThree);
		}

		if (this.ddlLevelFour.value == '')
		{
			filter.pLevel = 4;
			this.reqGetList.Call (filter, this.ddlLevelFour);
		}

		if (this.ddlLevelFive.value == '')
		{
			filter.pLevel = 5;
			this.reqGetList.Call (filter, this.ddlLevelFive);
		}
	},


	// Search

	OnSearch : function ()
	{
		this.currentPageIndex = 0;
		this.currentQueryText = this.txtQueryText.value;

		this.DoSearch ();
	},

	GetListFilter : function ()
	{
		var result =  
		{
			pMonitoredDirectoryID : this.ddlMonitoredDirectory.value,
			pSubCatalogID : this.ddlSubCatalog.value,
			
			pLevel_1 : this.ddlLevelOne.value,
			pLevel_2 : this.ddlLevelTwo.value,
			pLevel_3 : this.ddlLevelThree.value,
			pLevel_4 : this.ddlLevelFour.value,
			pLevel_5 : this.ddlLevelFive.value,
			pLevel_6 : this.txtLevelSix.value,

			pMaxLevelUsed : 6 
		};

		return result;
	},

	DoSearch : function ()
	{
		var filter;


		this.EnableSearch (false);
		
		this.divSearchResults.innerHTML = '<div class="loadingMessage">Procesando su solicitud, espere un momento...</div>';

		filter = this.GetListFilter ();

		filter.pGeneralQuery = this.currentQueryText;
		filter.pEnterpriseName = this.txtEnterpriseName.value;
		filter.pEnterpriseIdentificationNumber = this.txtEnterpriseIdentificationNumber.value;

		filter.pPageIndex = this.currentPageIndex;
		filter.pPageSize = this.currentPageSize;

		this.reqSearch.Call (filter, this);
	},
	
	OnSearchComplete : function (pResponse, pProcessingDurationms, pLocalParams)
	{
		var xmlDoc = LoadXMLDocument (pResponse);
		
		var frmSearch = pLocalParams;
		

		frmSearch.currentPageCount = 0;
		
		if (IsIE)
		{
			if (xmlDoc.documentElement.childNodes.length > 0)
			{
				frmSearch.currentPageCount = Math.ceil (parseInt (xmlDoc.documentElement.childNodes [0].childNodes [xmlDoc.documentElement.childNodes [0].childNodes.length - 1].attributes [0].value) / frmSearch.currentPageSize);
			}		   
		}
		else
		{
			frmSearch.currentPageCount = Math.ceil (xmlDoc.evaluate ("/FRs/FR[1]/P/@RCount", xmlDoc, null, XPathResult.NUMBER_TYPE, null).numberValue / frmSearch.currentPageSize);
		}
		
		
		frmSearch.divSearchResults.innerHTML = 
			Transform_XML_URLXSLT (
				xmlDoc, 
				"xslt/searchResultsGrid.xslt",
			{	
				pPageIndex : frmSearch.currentPageIndex,
				pPageSize : frmSearch.currentPageSize,
				pClientProcessingDurationms : pProcessingDurationms
			});
			

		frmSearch.EnableSearch (true);
		
		FixLinksFrame ();
		FixContentSizes ();
	},

	OnPageFirst : function ()
	{
		this.currentPageIndex = 0;

		this.DoSearch ();
	},

	OnPageNext : function ()
	{
		this.currentPageIndex ++;

		this.DoSearch ();
	},

	OnPagePrevious : function ()
	{
		this.currentPageIndex --;

		this.DoSearch ();
	},

	OnPageLast : function ()
	{
		this.currentPageIndex = this.currentPageCount - 1;

		this.DoSearch ();
	},


	ShowResultsPageIndex : function (pArrayOf_URLMD5_pItemCount_OCRHits)
	{
		var items = new Array ();
		var tmpItems;
		
		
		tmpItems = eval ("[" + pArrayOf_URLMD5_pItemCount_OCRHits + "]");
		
		
		items.push (tmpItems [0]);

		if (!IsNull (tmpItems [0].Children))
		{
			for (var i = 0; i < tmpItems [0].Children.length; i++)
			{
				// push
				items.unshift  (tmpItems [0].Children [i]);
			}
		}

		esfImageGallery.Reset ();

		esfImageGallery.URLMD5_pItemCount_OCRHits = items;

		esfImageGallery.ImageSizeFactor = 0.3;

		esfImageGallery.BaseURLFormat = esf_RelativePath + "ImageTumbnail.ashx?ImagePath=~/Temp/ExtractImages/{0}/{1}.png&ImageWidth={2}&ImageHeight={3}";
		
		esfImageGallery.ShowImage ();
	}
}